home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mntlb20 / lib / isatty.c < prev    next >
C/C++ Source or Header  |  1991-04-15  |  1KB  |  44 lines

  1. /* from the original GCC TOS library by jrd */
  2. /* this algorithm is due to Allan Pratt @ Atari.  Thanks Allan! */
  3.  
  4. #include <errno.h>
  5. #include <osbind.h>
  6. #include <fcntl.h>
  7. #include <stdio.h>
  8. #include <ioctl.h>
  9.  
  10. struct __open_file __open_stat[__NHANDLES];
  11.  
  12. int isatty(fd)
  13. int fd;
  14. {
  15.   int rc;
  16.   long oldloc, seekval;
  17.   int handle = __OPEN_INDEX(fd);
  18.  
  19.   if (handle < __NHANDLES)
  20.     if (__open_stat[handle].status != FH_UNKNOWN)
  21.         return(__open_stat[handle].status == FH_ISATTY);
  22.   oldloc = Fseek (0L, fd, SEEK_CUR);    /* seek zero bytes from current loc */
  23.   if (seekval = Fseek (1L, fd, SEEK_SET))    /* try to seek ahead one byte */
  24.     if ((seekval > 0) || (seekval == ((long)(-EBADARG)))) /* out of range... */
  25.       rc = 0;            /* file, not a tty */
  26.     else 
  27.       {
  28.       errno = EBADF;        /* any other error returns "invalid handle" */
  29.                 /* because you can't tell */
  30.       rc = 0;
  31.       }
  32.   else
  33.     rc = 1;            /* yes, tty (Fseek returns 0 only for ttys) */
  34.   (void)Fseek(oldloc, fd, SEEK_SET);    /* seek back to original location */
  35.   if (handle < __NHANDLES)
  36.     if (rc) {
  37.         __open_stat[handle].status = FH_ISATTY;
  38.         __open_stat[handle].flags = CRMOD|ECHO;
  39.     }
  40.     else
  41.         __open_stat[handle].status = FH_ISAFILE;
  42.   return (rc);            /* return true, false, or error */
  43. }
  44.